<?php


/**
 * TimeSetting.php
 *
 * Class that stores the time limit on a given project
 *
 * @author Jose Oliveira <jafo@ua.pt> 2012
 */



class TimeSetting{

	private $timeForQuest;
	private $mandatoryTQ;
	private $timeForArg;
	private $mandatoryTA;
	private $projectID;

	/**
	 * @param integer $timeForQuest sets the maximum time allowed in the questioning process
	 * @param integer $mandatoryTQ sets if the project Admin wants to force the users to respect the questioning time
	 * @param integer $timeForArg sets the maximum time allowed in the argumentation process
	 * @param integer $mandatoryTA sets if the project Admin wants to force the users to respect the argumentation time
	 * @param integer $projectID 
	 */
	function __construct($timeForQuest, $mandatoryTQ, $timeForArg, $mandatoryTA, $projectID){

		if(isset($timeForQuest) && isset($timeForArg) && isset($mandatoryTQ) && isset($mandatoryTA) && isset($projectID)){
			$this->timeForQuest=$timeForQuest;
			$this->timeForArg=$timeForArg;
			$this->mandatoryTQ=$mandatoryTQ;
			$this->mandatoryTA=$mandatoryTA;
			$this->projectID=$projectID;
		}

	}


	/**
	 * Method used to store the time criteria established by a project administrator when creating a project
	 * 
	 * @throws Exception
	 * @return bool
	 */
	public function saveNewSettings(){

		$conn=new ConnDBUser();
		$conn->connect();

		$result=mysql_query("insert into timeSetting(timeForQuest, timeForArg, mandatoryTQ, mandatoryTA, projectID) values(
		" . $this->timeForQuest . ", " . $this->timeForArg . ", " . $this->mandatoryTQ . ", " . $this->mandatoryTA . ",
		 " . $this->projectID . ")");

		if(!$result){
			throw new Exception("Lamentamos mas ocorreu um erro");
		}

		try{
			$conn->disconnect($result);
			return true;
		}catch (Exception $e){
			$e->getMessage();
		}

		$conn->disconnect($result);
		return false;
	}


	/**
	 * Method used to store the criteria established by a user when updates an already existing project
	 * 
	 * @throws Exception
	 * @return bool
	 */
	public function updateSettings(){

		$conn=new ConnDBUser();
		$conn->connect();

		$result=mysql_query("update timeSetting set timeForQuest=" . $this->timeForQuest . ", timeForArg=" . $this->timeForArg . ",
		mandatoryTQ=" . $this->mandatoryTQ . ", mandatoryTA=" . $this->mandatoryTA . " where projectID=" . $this->projectID . "");
			
		if(!$result){
			throw new Exception("Lamentamos mas ocorreu um erro");
		}

		try{
			$conn->disconnect($result);
			return true;
		}catch (Exception $e){
			$e->getMessage();
		}

		$conn->disconnect($result);
		return false;
	}

	/**
	 * Method that getting a projectID, returns the respective time settings
	 * @param unknown_type $projectID
	 * @throws Exception
	 */
	public function getSettings($projectID){

		$conn=new ConnDBUser();
		$conn->connect();

		$result=mysql_query("select * from timeSetting where projectID=$projectID");

		if(!$result){
			throw new Exception("Lamentamos mas ocorreu um erro");
		}

		try{
			$registo=mysql_fetch_array($result);
			$conn->disconnect($result);
			return $registo;
		}catch(Exception $e){
			$e->getMessage();
		}

		$conn->disconnect($result);
		return '';
	}
}

?>